-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.java
41 lines (32 loc) · 1.07 KB
/
Solution.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import java.util.Scanner;
public class FindDuplicates {
static void findDuplicates(int[] arr) {
System.out.print("Duplicate elements: ");
boolean found = false;
for (int i = 0; i < arr.length - 1; i++) {
for (int j = i + 1; j < arr.length; j++) {
if (arr[i] == arr[j]) {
System.out.print(arr[i] + " ");
found = true;
break; // Avoid printing the same duplicate multiple times
}
}
}
if (!found) {
System.out.print("None");
}
System.out.println();
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the number of elements in the array: ");
int n = scanner.nextInt();
int[] arr = new int[n];
System.out.println("Enter the elements of the array:");
for (int i = 0; i < n; i++) {
arr[i] = scanner.nextInt();
}
findDuplicates(arr);
scanner.close();
}
}